package logic

import (
	"context"
	"fmt"
	"strconv"
	"strings"

	"lottery-zero/external/award/ssq"
	"lottery-zero/external/utils"
	"lottery-zero/internal/svc"
	"lottery-zero/internal/types"

	"github.com/tal-tech/go-zero/core/logx"
)

type SsqcheckLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSsqcheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) SsqcheckLogic {
	return SsqcheckLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SsqcheckLogic) Ssqcheck(req types.SsqCheckReq) (*types.SsqCheckReply, error) {
	tick := ssq.SsqBall{Redboll: req.Redball, Blueboll: req.Blueball}
	retCheck := ssq.DBAll.AwardCheck(&tick)
	var (
		AA []string
		BA []string
		CA []string
		DA []string
		EA []string
	)
	for _, as := range *retCheck {
		blues := strconv.Itoa(as.Blueboll)
		if len(blues) == 1 {
			blues = "0" + blues
		}
		ss := fmt.Sprintf("时间 %s, 期号 %s, 红球: %v, 蓝球 %s", as.T.Format("2006-01-02"), as.Q, strings.Join(utils.BollIntToStrSlice(as.Redboll), " "), blues)
		if as.A > 0 {
			AA = append(AA, ss)
		} else if as.B > 0 {
			BA = append(BA, ss)
		} else if as.C > 0 {
			CA = append(CA, ss)
		} else if as.D > 0 {
			DA = append(DA, ss)
		} else if as.E > 0 {
			EA = append(EA, ss)
		}
	}
	rets := []types.SsqAwardLevel{}
	if len(AA) > 0 {
		rets = append(rets, types.SsqAwardLevel{Title: "一等奖", Data: AA})
	}
	if len(BA) > 0 {
		rets = append(rets, types.SsqAwardLevel{Title: "二等奖", Data: BA})
	}
	if len(CA) > 0 {
		rets = append(rets, types.SsqAwardLevel{Title: "三等奖", Data: CA})
	}
	if len(DA) > 0 {
		rets = append(rets, types.SsqAwardLevel{Title: "四等奖", Data: DA})
	}
	if len(EA) > 0 {
		rets = append(rets, types.SsqAwardLevel{Title: "五等奖", Data: EA})
	}

	return &types.SsqCheckReply{Data: rets}, nil
}
